package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Gameintel;
import cn.wolfcode.trip.base.mapper.GameintelMapper;
import cn.wolfcode.trip.base.service.IGameIntelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class GameIntelServiceImpl implements IGameIntelService {

    @Autowired
    private GameintelMapper mapper;


    @Override
    public void saveScore(Gameintel g) {
        //保存分数:先去查询是否存在?
        Long userId = g.getUserId();
        int score = g.getScore();
       /* Gameintel oldG = mapper.selectByUserId(userId);
        if (oldG == null) {
            //不存在就新建
            mapper.insert(g);
        } else {
            //否则新的分数为2者之和
            g.setId(oldG.getId());
            g.setScore(g.getScore() + oldG.getScore());
            mapper.updateByPrimaryKey(g);
        }*/
        //直接修改积分表
        Long maxId = mapper.selectMaxId(g.getUserId());
        Map map = mapper.selectOldSign(maxId);
        int integral = (int) map.get("integral");
        integral = integral + score;
        mapper.updateScore(integral, maxId);


    }

    @Override
    public Gameintel get(Long userId) {
        return mapper.selectByUserId(userId);
    }
}
